package com.hd.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

public class DateUtil {
    public static Date str2Date(String d){
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try {
            date=df.parse(d);
        }catch (Exception e){
            e.printStackTrace();
        }
        return date;
    }
    public static String date2Str(Date d){
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        String ss="";
        try {
            ss=df.format(d);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ss;
    }

    public static String date2Str2(Date d){
        SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");
        String ss="";
        try {
            ss=df.format(d);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ss;
    }

    public static String datetime2Str(Date d){
        SimpleDateFormat df=new SimpleDateFormat("yyyyMMddhhmmss");
        String ss="";
        try {
            ss=df.format(d);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ss;
    }
    public static String datetimeToStr(Date d){
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ss="";
        try {
            ss=df.format(d);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ss;
    }
   /* 
     * 将时间转换为时间戳
     */    
    public static String dateToStamp(String s) throws ParseException{
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /* 
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    public static void main(String[] args) {
        String s="1559541033041";
        String s1=stampToDate(s);
        System.out.println(s1);
    }
}
